

import UIKit

import WebKit

class AboutController: UIViewController {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadWebView()
        prepareUI()
        layoutUI()
    }
    
    func prepareUI() {
        self.edgesForExtendedLayout = .init(rawValue: 0)
        self.view.backgroundColor = .white
        self.view.addSubview(webview)
    }
    
    func layoutUI() {
        webview.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(0)
        }
    }
    
    func goBack() {
        if webview.canGoBack {
            self.webview.goBack()
        }
    }
    
    func loadWebView() {
        //创建网址
        let url = URL(string: ABOUT_URL)
        //创建请求
        let request = URLRequest(url: url!)
        //加载请求
        webview.load(request)
    }
    
    fileprivate lazy var webview: WKWebView = { [unowned self] in
        let object = WKWebView()
        object.scrollView.bounces = false
        object.navigationDelegate = self
        object.scrollView.showsVerticalScrollIndicator = false
        object.scrollView.showsHorizontalScrollIndicator = false
        return object
        }()
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension AboutController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.navigationItem.title = self.webview.title
    }
}

extension AboutController: WKUIDelegate {
    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确认", style: .default, handler: { (_) in
        }))
        self.present(alert, animated: true, completion: nil)
    }
}
